﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace RXY_Mall.Core
{
    public static class Validator
    {
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="_express">正则表达式</param>
        /// <param name="value">要验证的值</param>
        /// <returns>验证结果 布尔值</returns>
        public static bool QuickValidate(string _express, string value)
        {
            Regex regex = new Regex(_express);
            if (string.IsNullOrWhiteSpace(value))
            {
                return false;
            }
            return regex.IsMatch(value);
        }
        /// <summary>
        /// 验证是否为1-999  之间的数字
        /// </summary>
        /// <param name="value">要验证的数据</param>
        /// <returns>验证结果 布尔值</returns>
        public static bool IsNumber(string value)
        {
            return QuickValidate("^[1-9][0-9]?[0-9]?$", value);
        }
        /// <summary>
        /// 验证是否是数字字母的组合
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNumOrLetter(string value)
        {
            return QuickValidate("^[A-Za-z0-9]+$", value);
        }
        /// <summary>
        /// 验证是否为数字
        /// </summary>
        /// <param name="value">要验证的数据</param>
        /// <returns>验证结果 布尔值</returns>
        public static bool IsNum(string value)
        {
            return QuickValidate("^[0-9]*$", value);
        }
        public static bool IsDecimal(string value)
        {
            try
            {
                if (string.IsNullOrEmpty(value))
                    return false;
                Convert.ToDecimal(value);
                return true;
            }
            catch { return false; }
        }
        /// <summary>
        /// 判断一个字符串是否为Int
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsInt(string value)
        {
            try
            {
                if (string.IsNullOrEmpty(value))
                    return false;
                Convert.ToInt32(value);
                return true;
                //return QuickValidate(@"^(-){0,1}\d+$", value);
            }
            catch { return false; }
        }
        //验证日期格式是否正确
        public static bool IsTime(string time)
        {
            try
            {
                if (string.IsNullOrEmpty(time))
                    return false;
                Convert.ToDateTime(time);
                return true;
            }
            catch
            {
                return false;
            }
        }
        //验证日期格式是否正确
        public static bool IsDate(string d)
        {
            try
            {
                if (string.IsNullOrEmpty(d))
                    return false;
                DateTime.Parse(d);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 验证邮箱格式
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsEmail(string value)
        {
            try
            {//\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
                return QuickValidate(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([.]\w+)*$", value);
            }
            catch { return false; }
        }
        public static bool IsMobile(string value)
        {
            try
            {
                return QuickValidate(@"^(1)\d{10}", value);
            }
            catch
            {
                return false;
            }
        }
        public static bool IsMobile(string regex, string value)
        {
            try
            {
                return QuickValidate(regex, value);
            }
            catch
            {
                return false;
            }
        }
        public static bool IsBoolean(string value)
        {
            try
            {
                Boolean.Parse(value);
                return true;
            }
            catch { return false; }
        }

        public static bool CheckDataSet(DataSet ds)
        {
            return ((ds != null) && ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0)));
        }
    }
}
